# -*- coding: utf8 -*-
import csv
import requests
from lxml import etree
import sys,re
reload(sys)
sys.setdefaultencoding('utf-8')



def getWiki(name):
    lis = list()
	#get en wiki Introduce
	r = requests.get('https://en.wikipedia.org/wiki/'+name,timeout=5)
	Selector = etree.HTML(r.text)
	try:
		data = Selector.xpath('//*[@id="mw-content-text"]/p')[0]
		info = data.xpath('string(.)')
		content = info.replace('\n',' ')
		if 'Coordinates' in content:
			data = Selector.xpath('//*[@id="mw-content-text"]/p')[1]
			info = data.xpath('string(.)')
			content = info.replace('\n','')
		content = re.sub('\[.*?\]','',content)
	except Exception as e:
		print('%s%s'%(e,'1'))
		content =None
	lis.append(content)

	#get en wiki date
	try:
		content = Selector.xpath("//*/th[text()='Established']/parent::*/td")[0]
		content = content.xpath('string(.)')
		content = content.replace('\n',' ')
		# content = re.sub('\[.*?\]','',content)
		content = re.findall('[0-9]{4}',content)
		lis.append(content[0])
	except Exception as e:
		print('%s%s'%(e,'2'))
		content =None
		lis.append(None)

	

	#get en wiki zh url 
	url = None
	try:
		content = Selector.xpath("//*/a[@lang='zh']/@href")[0]
		url ="https:" + content
	except Exception as e:
		print('%s%s'%(e,'3'))
		content =None
	
	# lis.append(content)

	#get zh wiki
	if url != None:
		r = requests.get(url,timeout=5)
		Selector = etree.HTML(r.text)
		try:
			data = Selector.xpath('//*[@id="firstHeading"]')[0]
			info = data.xpath('string(.)')
			content = info.replace('\n',' ')
			content = re.sub('\[.*?\]','',content)
		except Exception as e:
			print('%s%s'%(e,'4'))
			content =None
		lis.append(content)

		#get zh wiki instruct 
		try:
			data = Selector.xpath('//*[@id="mw-content-text"]/p')[0]
			info = data.xpath('string(.)')
			content = info.replace('\n',' ')
			if '坐标' in content:
				data = Selector.xpath('//*[@id="mw-content-text"]/p')[1]
				info = data.xpath('string(.)')
				content = info.replace('\n','')
			content = re.sub('\[.*?\]','',content)
		except Exception as e:
			print('%s%s%s'%(e,'  ',name))
			content =None
			print name
		lis.append(content)
	else:
		lis.append(None)
		lis.append(None)


	return lis


if __name__ == "__main__":
	reader_zh = open('GlobalListcn.txt','r')
	reader_en = open('GlobalList.txt','r')
	writer = csv.writer(file('GlobalList_en_zh.csv', 'wb'))
	writer.writerow(['enname', 'zhname','wiki_zhname','data','enIns','zhIns'])


	lis = list()
	lis_zh = list()
	lis_en = list()
	for line in reader_zh:
		name = line.split('\n')[0].strip()
		lis_zh.append(name)

	for line in reader_en:
		name = line.split('\n')[0].strip()
		lis_en.append(name)
	for i in range(len(lis_en)):
		lis.append([lis_en[i],lis_zh[i]])

	for line in lis:
		while 1:
			try:
				ln = getWiki(line[0])
				writer.writerow([line[0],line[1],ln[2],ln[1],ln[0],ln[3]])
				print line[0]
				break
			except Exception as e:
				print e

		# break
	reader_zh.close()
	reader_en.close()
